<template>
  <div>
    <ul class="list" v-if="list.length > 0">
      <li v-for="(item, index) in list" :key="index" :class="'item' + index">
        <div class="listTop">
          <div class="orderNo">
            <span>订单编号：{{ item.orderNo }}</span>
          </div>
          <div class="orderTime">
            <span>下单时间：{{ item.createTime }}</span>
          </div>
          <div class="serveTime">
            <span>预约时间：{{ item.serviceStartTime }}</span>
          </div>
          <div class="orderOrigin">
            <span>订单来源：{{ orderOriginOptions[item.orderSource] }}</span>
          </div>
          <div
            style="margin-left:20px;color: #fc9e76;"
            class="details"
            @click="gotoDetail(item.orderNo)"
          >
            点击查看详情
          </div>
        </div>
        <div class="listBottom">
          <div class="serve">
            <div
              v-for="(item1, index1) in item.orderNameAndUrlList"
              :key="index1"
              style="width:100%;"
            >
              <div class="serveBox" v-if="showFlagArr[index][index1]">
                <div>
                  <img
                    :src="item1.attachUrl"
                    class="img"
                    style="vertical-align: middle;"
                  />
                </div>
                <div>
                  <span style="margin-left:10px;vertical-align: middle;">{{
                    item1.goodsTitle
                  }}</span>
                </div>
              </div>
            </div>
            <div
              v-if="item.orderNameAndUrlList.length > 1"
              style="margin-left:20px;cursor: pointer;"
            >
              <i
                class="el-icon-arrow-down"
                @click="upDown(index)"
                v-if="showUpAndDown[index]"
              ></i>
              <i
                class="el-icon-arrow-up"
                @click="upDown(index)"
                v-if="!showUpAndDown[index]"
              ></i>
            </div>
          </div>
          <div class="orderMoney">￥{{ item.totalAmount }}</div>
          <div class="orderTrueMoney">￥{{ item.paymentAmount }}</div>
          <div class="servePeopleNum">
            {{ goodsTypeOptions[item.orderItemType] }}
          </div>
          <div class="serveCity">
            <span>{{ item.receiverCity }}/{{ item.businessAreaName }}</span>
          </div>
          <div class="userInformation">
            <div class="name">{{ item.receiverName }}</div>
            <div class="telephoneNum">{{ item.receiverMobile }}</div>
          </div>
          <div class="serveAddress">{{ item.serviceAddress }}</div>
          <div class="todo">
            <div class="orderStatus">
              {{ orderStatusOptions[item.orderStatus] }}
            </div>
            <div
              v-if="
                item.orderStatus == '1' ||
                  item.orderStatus == '3' ||
                  item.orderStatus == '7' ||
                  item.orderStatus == '11' ||
                  item.orderStatus == '5' ||
                  item.orderStatus == '9' ||
                  item.orderStatus == '13' ||
                  item.orderStatus == '18'
              "
            >
              <el-button
                size="mini"
                v-if="
                  item.orderStatus == '1' ||
                    item.orderStatus == '3' ||
                    item.orderStatus == '7' ||
                    item.orderStatus == '11' ||
                    item.orderStatus == '18'
                "
                @click="cencel(item.orderNo, item.orderStatus)"
                >取消订单</el-button
              >

              <el-button
                size="mini"
                v-if="
                  item.orderStatus == '5' ||
                    item.orderStatus == '9' ||
                    item.orderStatus == '13'
                "
                @click="
                  agree(item.paySerialId, item.orderStatus, item.paymentAmount)
                "
                >同意退款</el-button
              >
            </div>
          </div>
        </div>
      </li>
    </ul>

    <!-- 暂无数据 -->
    <div class="nomessage" v-if="list.length == 0">暂无数据</div>

    <!-- 同意退款dialog -->
    <el-dialog
      v-if="list.length > 0"
      :visible.sync="agreedialogVisible"
      width="30%"
    >
      <span>是否确定同意该笔退款</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="agreedialogVisible = false">取 消</el-button>
        <el-button
          style="background-color:#fd804c;color: #fff;color: #fff;"
          @click="agreeOrder"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 取消订单dialog -->
    <el-dialog
      title="取消订单"
      :visible.sync="canceldialogFormVisible"
      width="400px"
    >
      <el-form :model="form">
        <el-form-item>
          <el-select v-model="form.reason" placeholder="请选择">
            <el-option
              label="联系不上买家了"
              value="联系不上买家了"
            ></el-option>
            <el-option
              label="买家误拍或重拍了"
              value="买家误拍或重拍了"
            ></el-option>
            <el-option
              label="买家没有诚意完成购买"
              value="买家没有诚意完成购买"
            ></el-option>
            <el-option label="其他" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            type="textarea"
            v-model="form.desc"
            placeholder="请输入其他内容"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="canceldialogFormVisible = false">取 消</el-button>
        <el-button
          style="background-color:#fd804c;color: #fff;color: #fff;"
          @click="cancelOrder"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { urlToBase64 } from "./urlToBase64";
const orderStatusOptions = {
  "1": "待支付",
  "2": "已取消", // 待支付-已取消
  "3": "待派单",
  "4": "已取消", //待派单-已取消
  "5": "退款中", // 待派单-退款中
  "6": "已退款", // 待派单-已退款
  "7": "已派单",
  "8": "已取消", // 已派单-已取消
  "9": "退款中", // 已派单-退款中
  "10": "已退款", // 已派单-已退款
  "11": "待服务",
  "12": "已取消", // 待服务-已取消
  "13": "退款中", // 待服务-退款中
  "14": "已退款", // 待服务-已退款
  "15": "待评价",
  "16": "已完结",
  "17": "已关闭",
  "18": "待预约",
  "19": "已删除"
};

const orderOriginOptions = {
  miniapp: "小程序",
  android: "Android",
  iOS: "ios"
};

const goodsTypeOptions = {
  "0": "单项商品",
  "1": "保洁体验卡",
  "2": "家务师体验卡",
  "3": "保洁周期卡",
  "4": "家务师周期卡"
};

export default {
  props: ["list"],
  data() {
    return {
      currentOrderNo: "",
      currentOrderstatus: "",
      currentPaymentAmount: "", // 当前实付金额
      currrntPaySerialId: "", // 当前订单的支付流水号
      orderStatusOptions,
      orderOriginOptions,
      agreedialogVisible: false,
      canceldialogFormVisible: false,
      form: {
        reason: "",
        desc: ""
      },
      // 控制多个服务信息的显示隐藏
      showFlagArr: [],
      showUpAndDown: [],
      goodsTypeOptions // 商品类型枚举
    };
  },
  methods: {
    agree(paySerialId, currentOrderstatus, paymentAmount) {
      // 同意退款
      this.agreedialogVisible = true;
      this.currrntPaySerialId = paySerialId;
      this.currentOrderstatus = currentOrderstatus;
      this.currentPaymentAmount = paymentAmount; // 当前订单的实付金额
    },
    agreeOrder() {
      this.agreedialogVisible = false;
      // 调用同意退款接口  刷新页面
      this.$http({
        url: "/hssorderadmin/order/memOrder/agreeRefund",
        method: "POST",
        data: {
          paySerialId: this.currrntPaySerialId,
          state: this.currentOrderstatus,
          paymentAmount: this.currentPaymentAmount
        }
      }).then(res => {
        if (res) {
          // 退款提交成功  刷新页面
          this.$router.go(0);
        }
      });
    },
    cencel(orderNo, currentOrderstatus) {
      this.canceldialogFormVisible = true;
      this.currentOrderNo = orderNo;
      this.currentOrderstatus = currentOrderstatus;
    },
    cancelOrder() {
      this.canceldialogFormVisible = false;
      // 调用取消订单    刷新页面
      this.$http({
        url: "/hssorderadmin/order/memOrder/cancelOrder",
        method: "get",
        params: {
          orderNo: this.currentOrderNo,
          cancelReason:
            this.form.reason == "4" ? this.form.desc : this.form.reason,
          state: this.currentOrderstatus
        }
      }).then(res => {
        if (res) {
          // 取消订单成功  刷新页面
          if (res.data) {
            this.$message.error(res.data);
          } else {
            this.$router.go(0);
          }
        }
      });
    },
    gotoDetail(orderNo) {
      // 跳转订单详情
      this.$router.push({
        path: "/orderdetails",
        query: {
          orderNo: orderNo
        }
      });
    },
    initFlag() {
      let arr = [];
      this.list.forEach((item1, index1) => {
        const newarr = [];
        item1.orderNameAndUrlList.forEach((item2, index2) => {
          if (index2 == 0) {
            newarr.splice(index2, 0, true);
          } else {
            newarr.splice(index2, 0, false);
          }
        });
        arr.splice(index1, 0, newarr);
      });
      this.showFlagArr = arr; // 图片的显示和隐藏
    },
    upDown(index) {
      this.showUpAndDown.splice(index, 1, !this.showUpAndDown[index]);
      //console.log(this.showUpAndDown)
      // false 为下拉 true为收起
      if (!this.showUpAndDown[index]) {
        // 下拉
        let arr = this.showFlagArr[index];
        arr = new Array(arr.length).fill(true);
        this.$set(this.showFlagArr, index, arr);
      } else {
        // 收起
        let arr1 = this.showFlagArr[index];
        arr1.forEach((item, index) => {
          if (index > 0) {
            arr1.splice(index, 1, false);
          }
        });
        this.$set(this.showFlagArr, index, arr1);
      }
    }
  },
  watch: {
    list: {
      handler(newvalue) {
        if (newvalue.length > 0) {
          this.initFlag();
          const length = Number(this.list.length);
          this.showUpAndDown = new Array(length).fill(true);
        }
      },
      deep: true
    }
  }
};
</script>
<style lang="scss" scoped>
.list {
  margin-top: 12px;
  background-color: #ebeef1;
  font-size: 14px;
  padding-bottom: 12px;
}
li {
  background-color: #fff;
  margin-top: 12px;
  padding-left: 20px;
  font-size: 14px;
  .listTop {
    padding-right: 10px;
    height: 50px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    border-bottom: 1px solid rgba(0, 0, 0, 0.35);
    .checkBox {
      position: absolute;
      left: -34px;
      top: 16px;
    }
    .orderNo {
      min-width: 200px;
    }
    .orderTime {
      min-width: 200px;
    }
    .serveTime {
      min-width: 200px;
    }
    .orderOrigin {
      min-width: 100px;
    }
    .details {
      min-width: 100px;
      cursor: pointer;
    }
  }
  .listBottom {
    text-align: center;
    padding-right: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .orderMoney,
    .orderTrueMoney,
    .servePeopleNum,
    .serveCity,
    .userInformation,
    .serveAddress,
    .todo {
      width: 75px;
    }
    .img {
      display: inline-block;
      width: 60px;
      height: 60px;
    }
    .name {
      text-align: center;
    }
  }
}
li.item0 {
  margin-top: none;
}
.serveAddress {
  // max-width: 100px;
  width: 100px !important;
}
.serve {
  width: 155px !important;
  text-align: left;
  .serveBox {
    display: flex;
    align-items: center;
    width: 100%;
    margin: 3px 0;
  }
}
.userInformation {
  width: 100px !important;
  .name {
    width: 100%;
  }
  .telephoneNum {
    width: 100%;
  }
}
.orderStatus {
  margin-bottom: 5px;
}
.nomessage {
  height: 48px;
  line-height: 48px;
  font-size: 14px;
  font-family: "Times New Roman", Times, serif;
  text-align: center;
  background-color: #fff;
}
</style>
<style lang="scss" scoped>
/deep/ .el-form-item__content .el-select {
  width: 100%;
}
/deep/ .el-dialog__body {
  padding-top: 0px;
  padding-bottom: 0px;
}
/deep/ .el-button {
  border-color: rgb(72, 135, 189);
  color: rgb(72, 135, 189);
}
</style>
